//
//  SFBaseController.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/5/19.
//

import UIKit

class SFBaseController: UIViewController {
    
    open var hideNavigationBar: Bool = false

    private(set) var shouldDisableGestureBack: Bool =  false
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setup()
        
    }
    
    func setup() {
        
        view.backgroundColor = kControllerColor
        if let nav = navigationController, nav.viewControllers.count > 1 {
            if navigationItem.leftBarButtonItem == nil {
                let backItem = UIBarButtonItem.init(customView: backBtn)
                navigationItem.leftBarButtonItem = backItem
            }
        }else {
            
        }
        
    }
    
    func popBack() {
        backBtnAction()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(hideNavigationBar, animated: false)
    }

    var backBtn: UIButton {
        let btn = UIButton.init(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        btn.setImage(UIImage(named: "icon_base_back_arrow"), for: .normal)
        btn.setImage(UIImage(named: "icon_base_back_arrow"), for: .selected)
        btn.setImage(UIImage(named: "icon_base_back_arrow"), for: [.selected,.highlighted])
        btn.contentHorizontalAlignment = .left
        btn.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
        return btn
    }
    
    @objc func backBtnAction() {
        guard let nav = navigationController else {
            if presentingViewController != nil {
                self.dismiss(animated: true, completion: nil)
            }
            return
        }
        nav.popViewController(animated: true)
    }
}
